package order.server.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger文档配置类 只在开发和测试环境初始化
 * @author Administrator
 * @version [版本号, 2017/11/3 0003]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Configuration
@PropertySource(value = "classpath:/order-swagger.properties")
@EnableSwagger2
public class Swagger2Config {

    @Value("${api-info.title}")
    private String title;

    @Value("${api-info.description}")
    private String description;

    @Value("${api-info.version}")
    private String version;

    @Value("${api-info.contact.name}")
    private String contactName;

    @Value("${api-info.contact.url}")
    private String contactUrl;

    @Value("${api-info.contact.email}")
    private String contactEmail;

    @Bean
    public Docket createRestApiDev() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("order.server.server.leadeon.hlj.cache"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * @Description: 创建swagger docket对象 并注册到spring
     * @param
     * @return Docket 对象
     * @author wudan-mac
     * @date 2017/4/11
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl("/redis").contact(new Contact(contactName,contactUrl,contactEmail))
                .version(version).licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }
}
